﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Globalization;
#endregion

namespace NetBay.Core.Tools
{
    public static class DateTools
    {
        /// <summary>
        /// Determines whether [are dates in range] [the specified start date to check].
        /// </summary>
        /// <param name="p_dtStartDateToCheck">The start date to check.</param>
        /// <param name="p_dtEndDateToCheck">The end date to check.</param>
        /// <param name="p_dtStartDate">The start date.</param>
        /// <param name="p_dtEndDate">The end date.</param>
        /// <returns>
        /// 	<c>true</c> if [are dates in range] [the specified start date to check]; otherwise, <c>false</c>.
        /// </returns>
        public static bool AreDatesInRange(
            DateTime p_dtStartDateToCheck,
            DateTime p_dtEndDateToCheck,
            DateTime p_dtStartDate,
            DateTime p_dtEndDate)
        {
            if (p_dtStartDateToCheck < p_dtStartDate)
            {
                if (p_dtEndDateToCheck < p_dtStartDate)
                    return false;
                return true;
            }
            if (p_dtEndDateToCheck > p_dtEndDate)
            {
                if (p_dtStartDateToCheck > p_dtEndDate)
                    return false;
                return true;
            }
            return true;
        }


        public static bool IsLastDayOfWeek(DateTime p_dtDate)
        {
            return (GetLastDayOfWeek(p_dtDate) == p_dtDate);
        }

        public static bool IsLastDayOfMonth(DateTime p_dtDate)
        {
            return (p_dtDate.Date == GetLastDayOfMonth(p_dtDate));
        }


        public static bool IsLastDayOfYear(DateTime p_dtDate)
        {
            return (p_dtDate.Date == GetLastDayOfYear(p_dtDate));
        }


        public static DateTime GetLastDayOfWeek(DateTime p_dtDate)
        {
            return (GetFirstDayOfWeek(p_dtDate).AddDays(6));
        }

        public static DateTime GetLastDayOfMonth(DateTime p_dtDate)
        {
            DateTime tmpDate = DateTime.MinValue;
            tmpDate = tmpDate.AddYears(p_dtDate.Year - 1);
            tmpDate = tmpDate.AddMonths(p_dtDate.Month);
            tmpDate = tmpDate.AddDays(-1).Date;
            return tmpDate;
        }

        public static DateTime GetLastDayOfYear(
            DateTime p_dtDate)
        {
            DateTime tmpDate = DateTime.MinValue;
            tmpDate = tmpDate.AddYears(p_dtDate.Year);
            tmpDate = tmpDate.AddDays(-1).Date;
            return tmpDate;
        }

        public static DateTime GetFirstHourOfDay(DateTime p_dtDate)
        {
            return DateTime.Parse(p_dtDate.Date.ToString());
        }

        public static DateTime GetFirstDayOfWeek(DateTime p_dtDate)
        {
            CultureInfo Ci = System.Globalization.CultureInfo.CurrentCulture;
            DateTime tmpDate = p_dtDate;
            for (int i = 0; i < 7; i++)
            {
                tmpDate = p_dtDate.Date.AddDays(-i);
                if (tmpDate.DayOfWeek == Ci.DateTimeFormat.FirstDayOfWeek)
                    break;
            }
            return tmpDate;
        }

        public static DateTime GetFirstDayOfMonth(
            DateTime p_dtDate)
        {
            DateTime tmpDate = DateTime.MinValue;
            tmpDate = tmpDate.AddYears(p_dtDate.Year - 1);
            tmpDate = tmpDate.AddMonths(p_dtDate.Month - 1);
            return tmpDate;
        }

        public static DateTime GetFirstDayOfYear(
            DateTime p_dtDate)
        {
            DateTime tmpDate = DateTime.MinValue;
            tmpDate = tmpDate.AddYears(p_dtDate.Year - 1);
            return tmpDate;
        }

        public static int GetWeekNumber(
            CultureInfo p_oCulture,
            DateTime p_dtDate)
        {
            return p_oCulture.Calendar.GetWeekOfYear(p_dtDate,
                                           p_oCulture.DateTimeFormat.CalendarWeekRule,
                                           p_oCulture.DateTimeFormat.FirstDayOfWeek);
        }

        public static int GetWeekNumber(
            DateTime p_dtDate)
        {
            CultureInfo culture = System.Globalization.CultureInfo.CurrentCulture;
            return culture.Calendar.GetWeekOfYear(
                p_dtDate,
                culture.DateTimeFormat.CalendarWeekRule,
                culture.DateTimeFormat.FirstDayOfWeek);
        }

        public static void GetPeriodFromWeek(CultureInfo p_oCulture, int p_iWeek, int p_iYear, ref DateTime p_dtStartDate, ref DateTime p_dtEndDate)
        {
            int dayNumber = (p_iWeek - 1) * 7;
            DateTime tmpDate = new DateTime(p_iYear, 1, 1).AddDays(dayNumber);
            DayOfWeek dayOfWeek = p_oCulture.Calendar.GetDayOfWeek(tmpDate);
            DateTime lastDayOfYear = new DateTime(
                p_iYear,
                p_oCulture.Calendar.GetMonthsInYear(p_iYear),
                p_oCulture.Calendar.GetDaysInMonth(p_iYear, p_oCulture.Calendar.GetMonthsInYear(p_iYear)));

            short weekDay = Convert.ToInt16(dayOfWeek);
            short firstDayOfWeek =
                Convert.ToInt16(p_oCulture.DateTimeFormat.FirstDayOfWeek);
            if (dayOfWeek == DayOfWeek.Sunday)
                weekDay = 7;
            if (p_oCulture.DateTimeFormat.FirstDayOfWeek == DayOfWeek.Sunday)
                firstDayOfWeek = 7;

            p_dtStartDate = tmpDate.AddDays(-1 * (weekDay - firstDayOfWeek));
            p_dtEndDate = p_dtStartDate.AddDays(6);
            if (p_dtEndDate > lastDayOfYear)
                p_dtEndDate = lastDayOfYear;
        }
    }
}
